package org.qiyu.framework.protect.limit.controller;

import org.qiyu.framework.protect.limit.service.impl.SlidingWindowCounterLimit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author linhao
 * @Date created in 5:08 下午 2022/3/1
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    /**
     * 10秒内只能允许有10次限制流量
     */
    private static  SlidingWindowCounterLimit slidingWindowCounterLimit = new SlidingWindowCounterLimit(20,10);


    @GetMapping(value = "/do-test-a")
    public String doTestV2() {
        return "success";
    }

    @GetMapping(value = "/do-test")
    public String doTest() {
        if(!slidingWindowCounterLimit.tryCount()){
            return "fail";
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
